

import 'dart:convert';
import 'dart:math';

import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_db_tool.dart';
import 'package:bt_tool/basic/bt_model.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/basic/bt_utils.dart';
import 'package:bt_tool/widget/bt_toast.dart';
import 'package:flutter_demo/tools/project/model/project_model.dart';
import 'package:flutter_demo/tools/project/project_define.dart';
import 'package:flutter_demo/tools/translate/translate_define.dart';
import 'package:get/get.dart';

class TranslateConfigController extends BTController{

  ProjectModel? projectModel;

  var translateTargetList = <BTCommonModel>[].obs;
  var motherLanguageList = <BTCommonModel>[].obs;
  int pid = 0;

  @override
  void initState() async {
    super.initState();
    pid = routerJsonTool.getInt("pid");
    var dataMapList = await BTDbTool.share().query(ProjectDefine.tableName,where: "id = ?",whereArgs: [pid]);
    if(dataMapList.isEmpty){
      return;
    }
    projectModel = ProjectModel(dataMapList.first);
    List<String> configStrList = projectModel!.translateConfig.split(",");
    List<String> keyList = TranslateDefine.baseLanguageMapReverse.keys.toList();
    for(var key in keyList){
      translateTargetList.add(BTCommonModel.create(
        title: TranslateDefine.baseLanguageMapReverse[key] ?? "",
        isSelect: configStrList.contains(key),
        strValue: key,
      ));
      motherLanguageList.add(BTCommonModel.create(
          title: TranslateDefine.baseLanguageMapReverse[key] ?? "",
          isSelect: key == projectModel!.translateKey,
          strValue: key
      ));
    }
    updateWidget();

  }

  resetMotherLanguageData(){
    for(var model in motherLanguageList){
      model.isSelect = false;
    }
  }

  saveClick() async {
    String motherLanguage = "";
    for(var model in motherLanguageList){
      if(model.isSelect){
        motherLanguage = model.strValue;
        break;
      }
    }

    if(motherLanguage.isEmpty){
      BTToast.showToast("请选择母语");
      return;
    }

    String targetLanguage = "";
    for(var model in translateTargetList){
      if(model.isSelect){
        if(targetLanguage.isEmpty){
          targetLanguage = model.strValue;
        }else{
          targetLanguage = "$targetLanguage,${model.strValue}";
        }
      }
    }
    if(btIsStrEmpty(targetLanguage)){
      BTToast.showToast("请选择目标语言");
      return;
    }
    projectModel!.translateKey = motherLanguage;
    projectModel!.translateConfig = targetLanguage;
    var map = projectModel!.convertToDictOnly(["translateConfig","translateKey","translateExportType"]);
    var result = await BTDbTool.share().update(ProjectDefine.tableName, map,where: "id = ?",whereArgs: [pid]);
    if(result == BTDbTool.sqlSucAction){
      BTToast.showToast("配置成功");
      BTRouter.share().pop();
      routerConfig?.commonCallbackAction(map,id: "saveSuc");
      return;
    }
    BTToast.showToast("保存失败");
  }

}